from dashscope import Assistants
import os
import json
import sys

# 设置控制台输出编码为 UTF-8
sys.stdout.reconfigure(encoding="utf-8")

assistant = Assistants.create(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model="qwen-max",
    name="smart helper",
    description="A tool helper.",
    instructions="You are a helpful assistant. When asked a question, use tools wherever possible.",
    tools=[
        {"type": "search"},
        {
            "type": "function",
            "function": {
                "name": "big_add",
                "description": "Add to number",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "left": {"type": "integer", "description": "The left operator"},
                        "right": {
                            "type": "integer",
                            "description": "The right operator.",
                        },
                    },
                    "required": ["left", "right"],
                },
            },
        },
    ],
)

# 格式化输出助手信息
print("\n=== 助手信息 ===")
assistant_info = {
    "ID": assistant.id,
    "名称": assistant.name,
    "描述": assistant.description,
    "模型": assistant.model,
    "指令": assistant.instructions,
    "工具": [
        tool.__dict__ if hasattr(tool, "__dict__") else tool for tool in assistant.tools
    ],  # Convert tool objects to dictionaries
}


# Custom JSON encoder to handle non-serializable objects
class CustomJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        try:
            return obj.__dict__
        except AttributeError:
            return str(obj)


print(json.dumps(assistant_info, ensure_ascii=False, indent=2, cls=CustomJSONEncoder))
